package com.task;

import com.admin.entity.Apk;
import com.admin.entity.Fileinfo;
import com.admin.mapper.ApkMapper;
import com.admin.mapper.FileinfoMapper;
import com.aliyun.OssUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

/**
 * 定时上传文件到oss
 */
@SuppressWarnings("ALL")
@Component
public class UploadOssTask {

    @Autowired
    private OssUpload ossUpload;

    @Autowired
    private FileinfoMapper fileDAO;

    @Autowired
    private ApkMapper apkDAO;

    @Value("${upload.dir}")
    private String baseDIR;

    //@Scheduled(cron = "0 0 5 * * ?")
    public void uploadOSS() {
        //上传图片，视屏
        List<Fileinfo> files = fileDAO.selectList(null);
        if (files != null) {
            for (Fileinfo f : files) {
                String path = baseDIR + f.getUrl();
                File file = new File(path);
                if (file.exists()) {
                    ossUpload.upload(f.getUrl(), file);
                }
            }
        }
        //上传apk
        List<Apk>  apks = apkDAO.selectList(null);
        if (apks!=null)
        {
            for (Apk apk:apks)
            {
                String url = apk.getApkUrl();
                String tmpUrl = url;
                if (url!=null && url.startsWith("/upload"))
                {
                    tmpUrl = url.substring(7);
                }
                String path = baseDIR+tmpUrl;
                File file = new File(path);
                if (file.exists()) {
                    ossUpload.upload(url, file);
                }
            }
        }
    }
}
